package com.cosyhut.blocks.jode;

//import java.util.logging.Level;
//import com.jme.util.LoggingSystem;

import com.jme.math.*;

public abstract class ODESpatial {
    protected ODEBlock _block;

    protected SWIGTYPE_p_dxGeom _transform;
    protected SWIGTYPE_p_dxGeom _geom;

    protected  ODESpatial() {
        this._transform = jode.dCreateGeomTransform(null);
        jode.dGeomTransformSetInfo(this._transform, 1);
    }

    protected void setPosition(Vector3f p) {
        jode.dGeomSetPosition(this._geom, p.x, p.y, p.z);
    }

    protected void setRotation(Matrix3f r) {
        jode.dGeomSetRotation(this._geom, ODEHelper.fromMatrix3ftoArray(r));
    }


    // --------------------------------------------------------------------------
    public Vector3f getPosition() {
        SWIGTYPE_p_float result = jode.dGeomGetPosition(this._geom);
        return ODEHelper.fromArraytoVector3f(result);
    }

    public Vector3f getWorldPosition() {
        SWIGTYPE_p_float gp = jode.dGeomGetPosition(this._geom);
        SWIGTYPE_p_float result = jode.new_floatArray(3);
        
		jode.dBodyGetRelPointPos (this._block.getBody(), 
                jode.floatArray_getitem(gp, 0), 
                jode.floatArray_getitem(gp, 1), 
                jode.floatArray_getitem(gp, 2), 
                result);
		
        return ODEHelper.fromArraytoVector3f(result);
    }

    public Matrix3f getRotation() {
        SWIGTYPE_p_float result = jode.dGeomGetRotation(this._geom);
        return ODEHelper.fromArraytoMatrix3f(result);
    }

    public Matrix3f getWorldRotation() {
        SWIGTYPE_p_float gr = jode.dGeomGetRotation(this._geom);
        SWIGTYPE_p_float result = jode.dGeomGetRotation(this._transform);
        
        return ODEHelper.fromArraytoMatrix3f(result).mult(ODEHelper.fromArraytoMatrix3f(gr));
    }

    public ODEBlock getBlock() {
    	return this._block;
    }
    //--------------------------------------------------------------------------
    protected abstract void attach(ODEBlock block, Vector3f position, Matrix3f rotation);
    protected abstract void detach();
}
